﻿module Sustainalytics.ProductInvolvement.Security {

    export class PIPermissionsService implements IPIPermissionsService {

        constructor(
            private $q: ng.IQService,
            private ClaimsAuthorizationService: Sustainalytics.Claims.ClaimsAuthorizationService) {
        }

        //util function
        public hasValidPIIndicatorClaim(resName: string, key: string, value: string): boolean {
            var ctx = this;

            var params: Sustainalytics.Claims.ResourceParameters[] = new Array();
            var resPIParam = new Sustainalytics.Claims.ResourceParameters();

            resPIParam.key = key;
            resPIParam.value = value;
            params.push(resPIParam);

            var validPI = ctx.ClaimsAuthorizationService.Validate('api/v1/' + resName , 'GET', params);

            return validPI;
        }

        //Company/PI claim && at least one of the 18 PI claims
        public hasCompanyPI(): boolean {
            var ctx = this;

            var params: Sustainalytics.Claims.ResourceParameters[] = new Array();

            var validCompanyReport = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'companyreport');

            var validAE = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'ae');
            var validAL = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'al');
            var validCW = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'cw');
            var validFA = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'fa');
            var validFL = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'fl');
            var validGB = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'gb');
            var validMC = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'mc');
            var validNP = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'np');
            var validPE = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'pe');
            var validAB = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'ab');
            var validSC = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'sc');

            //new
            var validPM = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'pm');
            var validPP = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'pp');
            var validAT = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'at');
            var validPL = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'pl');


            var atLeastOnePIClaim = validAE
                                || validAL
                                || validCW
                                || validFA
                                || validFL
                                || validGB
                                || validMC
                                || validNP
                                || validPE
                                || validAB
                                || validSC
                                || validPM
                                || validPP
                                || validAT
                                || validPL;

            return validCompanyReport && atLeastOnePIClaim;
        }


        //(Research/PI claim && at least one of the 18 PI claims) || Compliance/Sanctions claim
        public hasMultiCompanyPI(): boolean {
            var ctx = this;

            var validMultiCompanyReport = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'multicompanyreport');

            var validAE = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'ae');
            var validAL = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'al');
            var validCW = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'cw');
            var validFA = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'fa');
            var validFL = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'fl');
            var validGB = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'gb');
            var validMC = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'mc');
            var validNP = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'np');
            var validPE = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'pe');
            var validAB = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'ab');
            var validSC = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'sc');

            //new
            var validPM = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'pm');
            var validPP = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'pp');
            var validAT = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'at');
            var validPL = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'pl');

            var atLeastOnePIClaim = validAE
                                || validAL
                                || validCW
                                || validFA
                                || validFL
                                || validGB
                                || validMC
                                || validNP
                                || validPE
                                || validAB
                                || validSC
                                || validPM
                                || validPP
                                || validAT
                                || validPL;

            var validComplianceSanctionsClaim = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'sanctions');

            return (validMultiCompanyReport && atLeastOnePIClaim) || validComplianceSanctionsClaim;
        }
    }

    angular.module('Sustainalytics.GCCS').factory('PIPermissionsService', ($q, ClaimsAuthorizationService) => {
        return new PIPermissionsService($q, ClaimsAuthorizationService);
    });
} 